home *** CD-ROM | disk | FTP | other *** search
/ The CICA Windows Explosion! / The CICA Windows Explosion! - Disc 2.iso / programr / ole2book.zip / CHAP10.ZIP / CHAP10 / SCHMOO / POLYLINE.CPP < prev    next >
C/C++ Source or Header  |  1993-06-17  |  30KB  |  1,342 lines

  1. /*
  2.  * POLYLINE.CPP
  3.  * Last modiciation for Chapter 5.
  4.  *
  5.  * Implementation of the CPolyline class.
  6.  *
  7.  * Copyright (c)1993 Microsoft Corporation, All Rights Reserved
  8.  *
  9.  * Kraig Brockschmidt, Software Design Engineer
  10.  * Microsoft Systems Developer Relations
  11.  *
  12.  * Internet  :  kraigb@microsoft.com
  13.  * Compuserve:  >INTERNET:kraigb@microsoft.com
  14.  */
  15.  
  16.  
  17.  
  18. #include "schmoo.h"
  19.  
  20.  
  21.  
  22. /*
  23.  * CPolyline:CPolyline
  24.  * CPolyline::~CPolyline
  25.  *
  26.  * Constructor Parameters:
  27.  *  hInst           HINSTANCE of the application we're in.
  28.  */
  29.  
  30. CPolyline::CPolyline(HINSTANCE hInst)
  31.     : CWindow(hInst)
  32.     {
  33.     m_pAdv=NULL;
  34.     m_hWnd=NULL;
  35.     return;
  36.     }
  37.  
  38.  
  39. CPolyline::~CPolyline(void)
  40.     {
  41.     return;
  42.     }
  43.  
  44.  
  45.  
  46.  
  47.  
  48. /*
  49.  * CPolyline::FInit
  50.  *
  51.  * Purpose:
  52.  *  Instantiates a polyline window within a given parent.  The
  53.  *  parent may be a main application window, could be an MDI child
  54.  *  window. We really do not care.
  55.  *
  56.  * Parameters:
  57.  *  hWndParent      HWND of the parent of this window
  58.  *  pRect           LPRECT that this window should occupy
  59.  *  dwStyle         DWORD containing the window's style flags
  60.  *  uID             UINT ID to associate with this window
  61.  *  pAdv            LPCPolylineAdviseSink of the sink wanting our notifications.
  62.  *
  63.  * Return Value:
  64.  *  BOOL            TRUE if the function succeeded, FALSE otherwise.
  65.  */
  66.  
  67. BOOL CPolyline::FInit(HWND hWndParent, LPRECT pRect, DWORD dwStyle, UINT uID
  68.     , LPCPolylineAdviseSink pAdv)
  69.     {
  70.     m_hWnd=CreateWindowEx(WS_EX_NOPARENTNOTIFY, SZCLASSPOLYLINE
  71.         , SZCLASSPOLYLINE, dwStyle, pRect->left, pRect->top
  72.         , pRect->right-pRect->left, pRect->bottom-pRect->top
  73.         , hWndParent, (HMENU)uID, m_hInst, (LPVOID)this);
  74.  
  75.     if (NULL!=m_hWnd)
  76.         m_pAdv=pAdv;
  77.  
  78.     return (NULL!=m_hWnd);
  79.     }
  80.  
  81.  
  82.  
  83.  
  84.  
  85.  
  86.  
  87. /*
  88.  * CPolyline::New
  89.  *
  90.  * Purpose:
  91.  *  Cleans out and reinitializes the data to defaults.
  92.  *
  93.  * Parameters:
  94.  *  None
  95.  *
  96.  * Return Value:
  97.  *  None
  98.  */
  99.  
  100. void CPolyline::New(void)
  101.     {
  102.     UINT        i;
  103.  
  104.     m_pl.wVerMaj=VERSIONMAJOR;
  105.     m_pl.wVerMin=VERSIONMINOR;
  106.  
  107.     //Our rectangle is the size of our window's client area.
  108.     GetClientRect(m_hWnd, &m_pl.rc);
  109.  
  110.     //Clean out the POLYLINE structure and repaint the window.
  111.     for (i=0; i< CPOLYLINEPOINTS; i++)
  112.         {
  113.         m_pl.rgpt[i].x=0;
  114.         m_pl.rgpt[i].y=0;
  115.         }
  116.  
  117.     m_pl.cPoints      =0;
  118.     m_pl.rgbBackground=GetSysColor(COLOR_WINDOW);
  119.     m_pl.rgbLine      =GetSysColor(COLOR_WINDOWTEXT);
  120.     m_pl.iLineStyle   =PS_SOLID;
  121.  
  122.     InvalidateRect(m_hWnd, NULL, TRUE);
  123.     UpdateWindow(m_hWnd);
  124.  
  125.     //Inform the advise sink of this data change.
  126.     if (NULL!=m_pAdv)
  127.         m_pAdv->OnDataChange();
  128.  
  129.     return;
  130.     }
  131.  
  132.  
  133.  
  134.  
  135. /*
  136.  * CPolyline::Undo
  137.  *
  138.  * Purpose:
  139.  *  Reverses previous actions in a Polyline.
  140.  *
  141.  * Parameters:
  142.  *  None
  143.  *
  144.  * Return Value:
  145.  *  BOOL            TRUE if we can Undo more, FALSE otherwise.
  146.  */
  147.  
  148. BOOL CPolyline::Undo(void)
  149.     {
  150.     //Decrement the number of active points and repaint.
  151.     if (m_pl.cPoints > 0)
  152.         {
  153.         m_pl.cPoints--;
  154.         InvalidateRect(m_hWnd, NULL, TRUE);
  155.         UpdateWindow(m_hWnd);
  156.         }
  157.  
  158.     if (NULL!=m_pAdv)
  159.         m_pAdv->OnPointChange();
  160.  
  161.     //Return if we can undo any more.
  162.     return (0!=m_pl.cPoints);
  163.     }
  164.  
  165.  
  166.  
  167.  
  168.  
  169.  
  170. /*
  171.  * CPolyline::ReadFromStorage
  172.  *
  173.  * Purpose:
  174.  *  Loads our data (any known version) from an IStoraeg returning
  175.  *  the version number of the data or an error value.
  176.  *
  177.  * Parameters:
  178.  *  pIStorage       LPSTORAGE from which we'll read.
  179.  *
  180.  * Return Value:
  181.  *  LONG            Version number or negative POLYLINE_E_* value.
  182.  */
  183.  
  184. LONG CPolyline::ReadFromStorage(LPSTORAGE pIStorage)
  185.     {
  186.     POLYLINEDATA    pl;
  187.     ULONG           cb=-1;
  188.     ULONG           cbExpect=0;
  189.     LPSTREAM        pIStream;
  190.     HRESULT         hr;
  191.     LARGE_INTEGER   li;
  192.  
  193.     if (NULL==pIStorage)
  194.         return POLYLINE_E_READFAILURE;
  195.  
  196.     //Open the CONTENTS stream
  197.     hr=pIStorage->OpenStream("CONTENTS", 0, STGM_DIRECT | STGM_READ
  198.         | STGM_SHARE_EXCLUSIVE, 0, &pIStream);
  199.  
  200.     if (FAILED(hr))
  201.         return POLYLINE_E_READFAILURE;
  202.  
  203.     //Read version numbers and seek back to file beginning.
  204.     hr=pIStream->Read((LPVOID)&pl, 2*sizeof(WORD), &cb);
  205.  
  206.     if (FAILED(hr) || 2*sizeof(WORD)!=cb)
  207.         {
  208.         pIStream->Release();
  209.         return POLYLINE_E_READFAILURE;
  210.         }
  211.  
  212.     LISet32(li, 0);
  213.     pIStream->Seek(li, STREAM_SEEK_SET, NULL);
  214.  
  215.     /*
  216.      * For version 2.0, read the entire file.  For version 1.0 read
  217.      * the file up to CBPOLYLINEDATAVER10.  For anything else, give an
  218.      * error.
  219.      */
  220.  
  221.     switch (pl.wVerMaj)
  222.         {
  223.         case VERSIONMAJOR:  //2.x
  224.             switch (pl.wVerMin)
  225.                 {
  226.                 case VERSIONMINOR:  //2.0
  227.                     cbExpect=CBPOLYLINEDATA;
  228.                     break;
  229.  
  230.                 default:
  231.                     break;
  232.                 }
  233.             break;
  234.  
  235.         case 1: //1.x
  236.             switch (pl.wVerMin)
  237.                 {
  238.                 case 0:  //1.0
  239.                     cbExpect=CBPOLYLINEDATA10;
  240.                     break;
  241.  
  242.                 default:
  243.                     break;
  244.                 }
  245.             break;
  246.  
  247.         default:
  248.             break;
  249.         }
  250.  
  251.     if (0==cbExpect)
  252.         {
  253.         pIStream->Release();
  254.         return POLYLINE_E_UNSUPPORTEDVERSION;
  255.         }
  256.  
  257.     hr=pIStream->Read((LPVOID)&pl, cbExpect, &cb);
  258.     pIStream->Release();
  259.  
  260.     if (cbExpect!=cb)
  261.         return POLYLINE_E_READFAILURE;
  262.  
  263.     /*
  264.      * If we loaded successfully, make the data current.  By using DataSet
  265.      * we centralize our version upgrading.  We size the polyline window
  266.      * to the data AND notify the document so it sizes to the polyline.
  267.      */
  268.     DataSet(&pl, TRUE, TRUE);
  269.  
  270.     //Return what version we just loaded.
  271.     return MAKELONG(pl.wVerMin, pl.wVerMaj);
  272.     }
  273.  
  274.  
  275.  
  276.  
  277. /*
  278.  * CPolyline::WriteToStorage
  279.  *
  280.  * Purpose:
  281.  *  Ignorantly writes our current data into an storage in a particular
  282.  *  version.
  283.  *
  284.  * Parameters:
  285.  *  pIStorage       LPSTORAGE in which to save.
  286.  *  lVer            LONG providing version number Major (HI) and Minor (Low)
  287.  *
  288.  * Return Value:
  289.  *  LONG            A POLYLINE_E_* value.
  290.  */
  291.  
  292. LONG CPolyline::WriteToStorage(LPSTORAGE pIStorage, LONG lVer)
  293.     {
  294.     ULONG           cb;
  295.     ULONG           cbExpect=0;
  296.     WORD            wVerMaj=HIWORD(lVer);
  297.     WORD            wVerMin=LOWORD(lVer);
  298.     POLYLINEDATA    pl;
  299.     LPSTREAM        pIStream;
  300.     HRESULT         hr;
  301.  
  302.     if (NULL==pIStorage)
  303.         return POLYLINE_E_WRITEFAILURE;
  304.  
  305.     //Get a copy of our data in the version we're going to save.
  306.     DataGet(&pl, lVer);
  307.  
  308.     switch (wVerMaj)
  309.         {
  310.         case VERSIONMAJOR:  //2.x
  311.             switch (wVerMin)
  312.                 {
  313.                 case VERSIONMINOR:  //2.0
  314.                     cbExpect=CBPOLYLINEDATA;
  315.                     break;
  316.  
  317.                 default:
  318.                     break;
  319.                 }
  320.             break;
  321.  
  322.         case 1: //1.x
  323.             switch (wVerMin)
  324.                 {
  325.                 case 0:  //1.0
  326.                     cbExpect=CBPOLYLINEDATA10;
  327.                     break;
  328.  
  329.                 default:
  330.                     break;
  331.                 }
  332.             break;
  333.  
  334.         default:
  335.             break;
  336.         }
  337.  
  338.     if (0==cbExpect)
  339.         return POLYLINE_E_UNSUPPORTEDVERSION;
  340.  
  341.     hr=pIStorage->CreateStream("CONTENTS", STGM_DIRECT | STGM_CREATE
  342.         | STGM_WRITE | STGM_SHARE_EXCLUSIVE, 0, 0, &pIStream);
  343.  
  344.     if (FAILED(hr))
  345.         return POLYLINE_E_WRITEFAILURE;
  346.  
  347.     hr=pIStream->Write((LPVOID)&pl, cbExpect, &cb);
  348.     pIStream->Release();
  349.  
  350.     if (FAILED(hr) || cbExpect!=cb)
  351.         return POLYLINE_E_WRITEFAILURE;
  352.  
  353.     return POLYLINE_E_NONE;
  354.     }
  355.  
  356.  
  357.  
  358.  
  359.  
  360. /*
  361.  * CPolyline::ReadFromFile
  362.  *
  363.  * Purpose:
  364.  *  Loads our data (any known version) from a file handle returning
  365.  *  the version number of the data or an error value.
  366.  *
  367.  * Parameters:
  368.  *  pszFile         LPSTR of the file to open.
  369.  *
  370.  * Return Value:
  371.  *  LONG            Version number or negative POLYLINE_E_* value.
  372.  */
  373.  
  374. LONG CPolyline::ReadFromFile(LPSTR pszFile)
  375.     {
  376.     OFSTRUCT        of;
  377.     HFILE           hFile;
  378.     POLYLINEDATA    pl;
  379.     UINT            cb=-1;
  380.     UINT            cbExpect=0;
  381.  
  382.     if (NULL==pszFile)
  383.         return POLYLINE_E_READFAILURE;
  384.  
  385.     hFile=OpenFile(pszFile, &of, OF_READ);
  386.  
  387.     if (HFILE_ERROR==hFile)
  388.         return POLYLINE_E_READFAILURE;
  389.  
  390.     //Read version numbers and seek back to file beginning.
  391.     cb=_lread(hFile, (LPSTR)&pl, 2*sizeof(WORD));
  392.     _llseek(hFile, 0L, 0);
  393.  
  394.     if (2*sizeof(WORD)!=cb)
  395.         {
  396.         _lclose(hFile);
  397.         return POLYLINE_E_READFAILURE;
  398.         }
  399.  
  400.     /*
  401.      * For version 2.0, read the entire file.  For version 1.0 read
  402.      * the file up to CBPOLYLINEDATAVER10.  For anything else, give an
  403.      * error.
  404.      */
  405.  
  406.     switch (pl.wVerMaj)
  407.         {
  408.         case VERSIONMAJOR:  //2.x
  409.             switch (pl.wVerMin)
  410.                 {
  411.                 case VERSIONMINOR:  //2.0
  412.                     cbExpect=CBPOLYLINEDATA;
  413.                     break;
  414.  
  415.                 default:
  416.                     break;
  417.                 }
  418.             break;
  419.  
  420.         case 1: //1.x
  421.             switch (pl.wVerMin)
  422.                 {
  423.                 case 0:  //1.0
  424.                     cbExpect=CBPOLYLINEDATA10;
  425.                     break;
  426.  
  427.                 default:
  428.                     break;
  429.                 }
  430.             break;
  431.  
  432.         default:
  433.             break;
  434.         }
  435.  
  436.     if (0==cbExpect)
  437.         {
  438.         _lclose(hFile);
  439.         return POLYLINE_E_UNSUPPORTEDVERSION;
  440.         }
  441.  
  442.     cb=_lread(hFile, (LPSTR)&pl, cbExpect);
  443.     _lclose(hFile);
  444.  
  445.     if (cbExpect!=cb)
  446.         return POLYLINE_E_READFAILURE;
  447.  
  448.     /*
  449.      * If we loaded successfully, make the data current.  By using DataSet
  450.      * we centralize our version upgrading.  We size the polyline window
  451.      * to the data AND notify the document so it sizes to the polyline.
  452.      */
  453.     DataSet(&pl, TRUE, TRUE);
  454.  
  455.     //Return what version we just loaded.
  456.     return MAKELONG(pl.wVerMin, pl.wVerMaj);
  457.     }
  458.  
  459.  
  460.  
  461.  
  462.  
  463.  
  464. /*
  465.  * CPolyline::WriteToFile
  466.  *
  467.  * Purpose:
  468.  *  Ignorantly writes our current data into an opened file in a particular
  469.  *  version.
  470.  *
  471.  * Parameters:
  472.  *  pszFile         LPSTR filename in which to store the data.
  473.  *  lVer            LONG providing version number Major (HI) and Minor (Low)
  474.  *
  475.  * Return Value:
  476.  *  LONG            A POLYLINE_E_* value.
  477.  */
  478.  
  479. LONG CPolyline::WriteToFile(LPSTR pszFile, LONG lVer)
  480.     {
  481.     OFSTRUCT        of;
  482.     HFILE           hFile;
  483.     UINT            cb;
  484.     UINT            cbExpect=0;
  485.     WORD            wVerMaj=HIWORD(lVer);
  486.     WORD            wVerMin=LOWORD(lVer);
  487.     POLYLINEDATA    pl;
  488.  
  489.     if (NULL==pszFile)
  490.         return POLYLINE_E_WRITEFAILURE;
  491.  
  492.     //Get a copy of our data in the version we're going to save.
  493.     DataGet(&pl, lVer);
  494.  
  495.     switch (wVerMaj)
  496.         {
  497.         case VERSIONMAJOR:  //2.x
  498.             switch (wVerMin)
  499.                 {
  500.                 case VERSIONMINOR:  //2.0
  501.                     cbExpect=CBPOLYLINEDATA;
  502.                     break;
  503.  
  504.                 default:
  505.                     break;
  506.                 }
  507.             break;
  508.  
  509.         case 1: //1.x
  510.             switch (wVerMin)
  511.                 {
  512.                 case 0:  //1.0
  513.                     cbExpect=CBPOLYLINEDATA10;
  514.                     break;
  515.  
  516.                 default:
  517.                     break;
  518.                 }
  519.             break;
  520.  
  521.         default:
  522.             break;
  523.         }
  524.  
  525.     if (0==cbExpect)
  526.         return POLYLINE_E_UNSUPPORTEDVERSION;
  527.  
  528.     hFile=OpenFile(pszFile, &of, OF_CREATE | OF_WRITE);
  529.  
  530.     if (HFILE_ERROR==hFile)
  531.         return DOCERR_COULDNOTOPEN;
  532.  
  533.     cb=_lwrite(hFile, (LPSTR)&pl, cbExpect);
  534.     _lclose(hFile);
  535.  
  536.     return (cbExpect==cb) ? POLYLINE_E_NONE : POLYLINE_E_WRITEFAILURE;
  537.     }
  538.  
  539.  
  540.  
  541.  
  542.  
  543.  
  544.  
  545.  
  546.  
  547. /*
  548.  * CPolyline::DataSet
  549.  *
  550.  * Purpose:
  551.  *  Sets the current data in this Polyline to a given structure.
  552.  *
  553.  * Parameters:
  554.  *  ppl             LPPOLYLINEDATA to initialize to.
  555.  *  fSizeToData     BOOL indicating if we're to size to the data or scale it.
  556.  *  fNotify         BOOL indicating if we're to send an advise on this change.
  557.  *
  558.  * Return Value:
  559.  *  LONG            A POLYLINE_E_* value.
  560.  */
  561.  
  562. LONG CPolyline::DataSet(LPPOLYLINEDATA ppl, BOOL fSizeToData, BOOL fNotify)
  563.     {
  564.     RECT        rc;
  565.     UINT        i;
  566.  
  567.     /*
  568.      * Copy the structure in ppl and repaint to reflect the new point
  569.      * set.  Note that unlike the RectSet message, we do no scaling,
  570.      * assuming that the rect in the structure is appropriate for the data.
  571.      */
  572.  
  573.     if (NULL==ppl)
  574.         return POLYLINE_E_INVALIDPOINTER;
  575.  
  576.     //Preserve the old rectangle
  577.     rc=m_pl.rc;
  578.  
  579.     /*
  580.      * For version 2.0 we perform a straight copy.  For version 1.0 we
  581.      * copy the 1.0 structure and fill in defaults for the 2.0 additions.
  582.      */
  583.  
  584.     switch (ppl->wVerMaj)
  585.         {
  586.         case VERSIONMAJOR:          //2.x
  587.             switch (ppl->wVerMin)
  588.                 {
  589.                 case VERSIONMINOR:  //2.0
  590.                     m_pl=*ppl;
  591.                     break;
  592.  
  593.                 default:
  594.                     return POLYLINE_E_UNSUPPORTEDVERSION;
  595.                 }
  596.             break;
  597.  
  598.         case 1:                     //1.x
  599.             switch (ppl->wVerMin)
  600.                 {
  601.                 case 0:             //1.0
  602.                     *((LPPOLYLINEDATA10)&m_pl)=*((LPPOLYLINEDATA10)ppl);
  603.  
  604.                     /*
  605.                      * Update this structure to 2.0.  Note that we assume
  606.                      * whoever loaded us to save the loaded version so it
  607.                      * can later ask what version the user wants to save.
  608.                      */
  609.                     m_pl.wVerMaj=VERSIONMAJOR;
  610.                     m_pl.wVerMin=VERSIONMINOR;
  611.  
  612.                     /*
  613.                      * Version 1.0 stored rc in parent coordinates.
  614.                      * We need those now in our client coodinates.
  615.                      */
  616.                     OffsetRect(&m_pl.rc, -m_pl.rc.left, -m_pl.rc.top);
  617.  
  618.                     /*
  619.                      * 1.0 data had points relative to size of the
  620.                      * rectangle.  We need to scale these to 0-32767
  621.                      * independent of the rectangle for the version upgrade.
  622.                      */
  623.                     for (i=0; i < m_pl.cPoints; i++)
  624.                         PointScale(&m_pl.rc, &m_pl.rgpt[i], FALSE);
  625.  
  626.                     //New 2.0 features
  627.                     m_pl.rgbBackground=GetSysColor(COLOR_WINDOW);
  628.                     m_pl.rgbLine      =GetSysColor(COLOR_WINDOWTEXT);
  629.                     m_pl.iLineStyle   =PS_SOLID;
  630.                     break;
  631.  
  632.                 default:
  633.                     return POLYLINE_E_UNSUPPORTEDVERSION;
  634.                 }
  635.             break;
  636.  
  637.         default:
  638.             return POLYLINE_E_UNSUPPORTEDVERSION;
  639.         }
  640.  
  641.  
  642.     //Inform our parent of the data change
  643.     if (NULL!=m_pAdv)
  644.         m_pAdv->OnDataChange();
  645.  
  646.     /*
  647.      * If we're scaling the window to fit the data, then use
  648.      * RectSet passing our current rectangle as the new one.
  649.      * That makes sure that the data won't change but that the
  650.      * window is resized.
  651.      */
  652.  
  653.     if (fSizeToData)
  654.         {
  655.         POINT       pt;
  656.  
  657.         /*
  658.          * Get our offset in the parent window so we can RectSet
  659.          * to the right place since RectSet expects rectangle in
  660.          * parent coordinates and we get it in client coordinates.
  661.          */
  662.         GetWindowRect(m_hWnd, &rc);
  663.         pt.x=rc.left;
  664.         pt.y=rc.top;
  665.         ScreenToClient(GetParent(m_hWnd), &pt);
  666.         rc=m_pl.rc;
  667.         OffsetRect(&rc, pt.x, pt.y);
  668.  
  669.         //This will also cause a repaint.
  670.         RectSet(&rc, fNotify);
  671.         }
  672.     else
  673.         {
  674.         //Make sure we're updated.
  675.         InvalidateRect(m_hWnd, NULL, TRUE);
  676.         UpdateWindow(m_hWnd);
  677.         }
  678.  
  679.     return POLYLINE_E_NONE;
  680.     }
  681.  
  682.  
  683.  
  684.  
  685.  
  686.  
  687.  
  688. /*
  689.  * CPolyline::DataGet
  690.  *
  691.  * Purpose:
  692.  *  Retrieves the Polyline's current data.
  693.  *
  694.  * Parameters:
  695.  *  ppl             LPPOLYLINEDATA into which we copy the data.
  696.  *  lVer            LONG version of the data to retrieve.  Use VERSIONCURRENT
  697.  *                  to retrieve the most current data.
  698.  *
  699.  * Return Value:
  700.  *  LONG            A POLYLINE_E_* value
  701.  */
  702.  
  703. LONG CPolyline::DataGet(LPPOLYLINEDATA ppl, LONG lVer)
  704.     {
  705.     UINT        i;
  706.  
  707.     //Retieve the current version
  708.     if (lVer==MAKELONG(VERSIONMINOR, VERSIONMAJOR) || VERSIONCURRENT==lVer)
  709.         {
  710.         *ppl=m_pl;
  711.         return POLYLINE_E_NONE;
  712.         }
  713.  
  714.     //Check for versions we support, 1.x
  715.     if (HIWORD(lVer)!=1)
  716.         return POLYLINE_E_UNSUPPORTEDVERSION;
  717.  
  718.     if (LOWORD(lVer)==0)    //Check for 1.0
  719.         {
  720.         //Do 2.0 to 1.0 conversion
  721.         *((LPPOLYLINEDATA10)ppl)=*((LPPOLYLINEDATA10)&m_pl);
  722.  
  723.         //Get the parent coordinates of our rectangle (that RectGet provides)
  724.         RectGet(&ppl->rc);
  725.  
  726.         /*
  727.          * 1.0 data has points relative to size of the rectangle.  We need to
  728.          * scale these from 0-32767 so we have the right values.
  729.          */
  730.         for (i=0; i < ppl->cPoints; i++)
  731.             PointScale(&ppl->rc, &ppl->rgpt[i], TRUE);
  732.  
  733.         //Insure old version numbers.
  734.         ppl->wVerMaj=1;
  735.         ppl->wVerMin=0;
  736.  
  737.         ((LPPOLYLINEDATA10)ppl)->fDrawEntire=TRUE;
  738.         return POLYLINE_E_NONE;
  739.         }
  740.  
  741.     return POLYLINE_E_UNSUPPORTEDVERSION;
  742.     }
  743.  
  744.  
  745.  
  746.  
  747.  
  748.  
  749.  
  750.  
  751. /*
  752.  * CPolyline::DataSetMem
  753.  *
  754.  * Purpose:
  755.  *  Sets the Polyline's data using a global memory handle
  756.  *  instead of a pointer.
  757.  *
  758.  * Parameters:
  759.  *  hMem            HGLOBAL containing the data.
  760.  *  fFree           BOOL indicating if we're to free the data.  The memory
  761.  *                  will be freed regardless of any error returned from here.
  762.  *  fSizeToData     BOOL indicating if we're to size to the data or scale it.
  763.  *  fNotify         BOOL indicating if we're to send an advise on this change.
  764.  *
  765.  * Return Value:
  766.  *  LONG            A POLYLINE_E_* value.
  767.  */
  768.  
  769. LONG CPolyline::DataSetMem(HGLOBAL hMem, BOOL fFree, BOOL fSizeToData
  770.     , BOOL fNotify)
  771.     {
  772.     LPPOLYLINEDATA  ppl;
  773.     LONG            lRet=POLYLINE_E_INVALIDPOINTER;
  774.  
  775.     if (NULL!=hMem)
  776.         {
  777.         ppl=(LPPOLYLINEDATA)GlobalLock(hMem);
  778.         lRet=DataSet(ppl, fSizeToData, fNotify);
  779.  
  780.         GlobalUnlock(hMem);
  781.  
  782.         if (fFree)
  783.             GlobalFree(hMem);
  784.         }
  785.  
  786.     return lRet;
  787.     }
  788.  
  789.  
  790.  
  791.  
  792.  
  793.  
  794.  
  795. /*
  796.  * CPolyline::DataGetMem
  797.  *
  798.  * Purpose:
  799.  *  Retrieves the Polyline's data in a global memory handle.
  800.  *
  801.  * Parameters:
  802.  *  lVer            LONG version of data to retrieve.
  803.  *  phMem           HGLOBAL FAR * in which to store the handle.
  804.  *
  805.  * Return Value:
  806.  *  LONG            A POLYLINE_E_* value.
  807.  */
  808.  
  809. LONG CPolyline::DataGetMem(LONG lVer, HGLOBAL FAR *phMem)
  810.     {
  811.     HGLOBAL         hMem;
  812.     LPPOLYLINEDATA  ppl;
  813.     LONG            lRet=POLYLINE_E_NONE;
  814.  
  815.     if (NULL==phMem)
  816.         return POLYLINE_E_INVALIDPOINTER;
  817.  
  818.     hMem=GlobalAlloc(GMEM_DDESHARE | GMEM_MOVEABLE, CBPOLYLINEDATA);
  819.  
  820.     if (NULL!=hMem)
  821.         {
  822.         ppl=(LPPOLYLINEDATA)GlobalLock(hMem);
  823.         lRet=DataGet(ppl, lVer);
  824.         GlobalUnlock(hMem);
  825.  
  826.         if (POLYLINE_E_NONE!=lRet)
  827.             {
  828.             GlobalFree(hMem);
  829.             hMem=NULL;
  830.             }
  831.         }
  832.  
  833.     *phMem=hMem;
  834.     return lRet;
  835.     }
  836.  
  837.  
  838.  
  839.  
  840.  
  841.  
  842.  
  843. /*
  844.  * CPolyline::RectGet
  845.  *
  846.  * Purpose:
  847.  *  Returns the rectangle of the Polyline in parent coordinates.
  848.  *
  849.  * Parameters:
  850.  *  pRect           LPRECT in which to return the rectangle.
  851.  *
  852.  * Return Value:
  853.  *  None
  854.  */
  855.  
  856. void CPolyline::RectGet(LPRECT pRect)
  857.     {
  858.     RECT        rc;
  859.     POINT       pt;
  860.  
  861.     //Retrieve the size of our rectangle in parent coordinates.
  862.     GetWindowRect(m_hWnd, &rc);
  863.     pt.x=rc.left;
  864.     pt.y=rc.top;
  865.     ScreenToClient(GetParent(m_hWnd), &pt);
  866.  
  867.     SetRect(pRect, pt.x, pt.y, pt.x+(rc.right-rc.left)
  868.         , pt.y+(rc.bottom-rc.top));
  869.  
  870.     return;
  871.     }
  872.  
  873.  
  874.  
  875.  
  876.  
  877. /*
  878.  * CPolyline::SizeGet
  879.  *
  880.  * Purpose:
  881.  *  Retrieves the size of the Polyline in parent coordinates.
  882.  *
  883.  * Parameters:
  884.  *  pRect           LPRECT in which to return the size.  The right and
  885.  *                  bottom fields will contain the dimensions.
  886.  *
  887.  * Return Value:
  888.  *  None
  889.  */
  890.  
  891. void CPolyline::SizeGet(LPRECT pRect)
  892.     {
  893.     RectGet(pRect);
  894.     return;
  895.     }
  896.  
  897.  
  898.  
  899.  
  900.  
  901.  
  902. /*
  903.  * CPolyline::RectSet
  904.  *
  905.  * Purpose:
  906.  *  Sets a new rectangle for the Polyline which sizes to fit.
  907.  *
  908.  * Parameters:
  909.  *  pRect           LPRECT containing the new rectangle.
  910.  *  fNotify         BOOL indicating if we're to notify anyone of the change.
  911.  *
  912.  * Return Value:
  913.  *  None
  914.  */
  915.  
  916. void CPolyline::RectSet(LPRECT pRect, BOOL fNotify)
  917.     {
  918.     UINT        cx, cy;
  919.  
  920.     //Scale the points from our current size to the new size
  921.     cx=pRect->right-pRect->left;
  922.     cy=pRect->bottom-pRect->top;
  923.  
  924.     SetWindowPos(m_hWnd, NULL, pRect->left, pRect->top
  925.         , (UINT)cx, (UINT)cy, SWP_NOZORDER);
  926.  
  927.     if (fNotify && NULL!=m_pAdv)
  928.         m_pAdv->OnSizeChange();
  929.  
  930.     InvalidateRect(m_hWnd, NULL, TRUE);
  931.  
  932.     return;
  933.     }
  934.  
  935.  
  936.  
  937.  
  938.  
  939.  
  940.  
  941. /*
  942.  * CPolyline::SizeSet
  943.  *
  944.  * Purpose:
  945.  *  Sets a new size for the Polyline which sizes to fit.
  946.  *
  947.  * Parameters:
  948.  *  pRect           LPRECT containing the new rectangle.
  949.  *  fNotify         BOOL indicating if we're to notify anyone of the change.
  950.  *
  951.  * Return Value:
  952.  *  None
  953.  */
  954.  
  955. void CPolyline::SizeSet(LPRECT pRect, BOOL fNotify)
  956.     {
  957.     UINT        cx, cy;
  958.  
  959.     //Scale the points from our current size to the new size
  960.     cx=pRect->right-pRect->left;
  961.     cy=pRect->bottom-pRect->top;
  962.  
  963.     SetWindowPos(m_hWnd, NULL, 0, 0, (UINT)cx, (UINT)cy
  964.         , SWP_NOMOVE | SWP_NOZORDER);
  965.  
  966.     if (fNotify && NULL!=m_pAdv)
  967.         m_pAdv->OnSizeChange();
  968.  
  969.     InvalidateRect(m_hWnd, NULL, TRUE);
  970.  
  971.     return;
  972.     }
  973.  
  974.  
  975.  
  976.  
  977.  
  978.  
  979. /*
  980.  * CPolyline::ColorSet
  981.  *
  982.  * Purpose:
  983.  *  Changes for background or line color in the Polyline
  984.  *
  985.  * Parameters:
  986.  *  iColor          UINT index of the color to change.
  987.  *  cr              COLORREF new color to use.
  988.  *
  989.  * Return Value:
  990.  *  COLORREF        Previous color for the index iColor.
  991.  */
  992.  
  993. COLORREF CPolyline::ColorSet(UINT iColor, COLORREF cr)
  994.     {
  995.     COLORREF    crRet;
  996.  
  997.     switch (iColor)
  998.         {
  999.         case POLYLINECOLOR_BACKGROUND:
  1000.             crRet=m_pl.rgbBackground;
  1001.             m_pl.rgbBackground=cr;
  1002.             break;
  1003.  
  1004.         case POLYLINECOLOR_LINE:
  1005.             crRet=m_pl.rgbLine;
  1006.             m_pl.rgbLine=cr;
  1007.             break;
  1008.         }
  1009.  
  1010.     //If the color changed, repaint
  1011.     if (crRet!=cr)
  1012.         {
  1013.         if (NULL!=m_pAdv)
  1014.             m_pAdv->OnColorChange();
  1015.  
  1016.         InvalidateRect(m_hWnd, NULL, TRUE);
  1017.         UpdateWindow(m_hWnd);
  1018.         }
  1019.  
  1020.     return crRet;
  1021.     }
  1022.  
  1023.  
  1024.  
  1025.  
  1026.  
  1027.  
  1028.  
  1029. /*
  1030.  * CPolyline::ColorGet
  1031.  *
  1032.  * Purpose:
  1033.  *  Retrieves one of the colors currently in use by the Polyline.
  1034.  *
  1035.  * Parameters:
  1036.  *  iColor          UINT identifying the color of interest.
  1037.  *
  1038.  * Return Value:
  1039.  *  COLORREF        Current color for iColor.
  1040.  */
  1041.  
  1042. COLORREF CPolyline::ColorGet(UINT iColor)
  1043.     {
  1044.     COLORREF    crRet;
  1045.  
  1046.     crRet=(POLYLINECOLOR_BACKGROUND==iColor)
  1047.         ? m_pl.rgbBackground : m_pl.rgbLine;
  1048.  
  1049.     return crRet;
  1050.     }
  1051.  
  1052.  
  1053.  
  1054.  
  1055.  
  1056.  
  1057.  
  1058.  
  1059. /*
  1060.  * CPolyline::LineStyleSet
  1061.  *
  1062.  * Purpose:
  1063.  *  Changes the line style in use by the Polyline
  1064.  *
  1065.  * Parameters:
  1066.  *  iStyle          UINT style of the line to use.
  1067.  *
  1068.  * Return Value:
  1069.  *  UINT            Previous style.
  1070.  */
  1071.  
  1072. UINT CPolyline::LineStyleSet(UINT iStyle)
  1073.     {
  1074.     UINT        uRet=(UINT)m_pl.iLineStyle;
  1075.  
  1076.     //Validate the line style
  1077.     if (PS_SOLID==iStyle || PS_DASH==iStyle || PS_DOT==iStyle
  1078.         || PS_DASHDOT==iStyle || PS_DASHDOTDOT==iStyle)
  1079.         {
  1080.         m_pl.iLineStyle=iStyle;
  1081.  
  1082.         if (uRet!=(UINT)m_pl.iLineStyle)
  1083.             {
  1084.             if (NULL!=m_pAdv)
  1085.                 m_pAdv->OnLineStyleChange();
  1086.  
  1087.             InvalidateRect(m_hWnd, NULL, TRUE);
  1088.             UpdateWindow(m_hWnd);
  1089.             }
  1090.         }
  1091.  
  1092.     return uRet;
  1093.     }
  1094.  
  1095.  
  1096.  
  1097.  
  1098.  
  1099.  
  1100.  
  1101. /*
  1102.  * CPolyline::LineStyleGet
  1103.  *
  1104.  * Purpose:
  1105.  *  Retrieves the current line style in use in the Polyline
  1106.  *
  1107.  * Parameters:
  1108.  *  None
  1109.  *
  1110.  * Return Value:
  1111.  *  UINT            Current line style.
  1112.  */
  1113.  
  1114. UINT CPolyline::LineStyleGet(void)
  1115.     {
  1116.     return m_pl.iLineStyle;
  1117.     }
  1118.  
  1119.  
  1120.  
  1121.  
  1122.  
  1123.  
  1124. /*
  1125.  * CPolyline::RenderBitmap
  1126.  *
  1127.  * Purpose:
  1128.  *  Creates a bitmap image of the current Polyline.
  1129.  *
  1130.  * Parameters:
  1131.  *  None
  1132.  *
  1133.  * Return Value:
  1134.  *  HBITMAP         Handle to the newly rendered bitmap.
  1135.  */
  1136.  
  1137. HBITMAP CPolyline::RenderBitmap(void)
  1138.     {
  1139.     HDC         hDC;
  1140.     HDC         hMemDC;
  1141.     HBITMAP     hBmp;
  1142.     RECT        rc;
  1143.     HGDIOBJ     hObj;
  1144.  
  1145.     //Render a bitmap the size of the current rectangle.
  1146.     hDC=GetDC(m_hWnd);
  1147.     hMemDC=CreateCompatibleDC(hDC);
  1148.  
  1149.     GetClientRect(m_hWnd, &rc);
  1150.     hBmp=CreateCompatibleBitmap(hDC, rc.right, rc.bottom);
  1151.  
  1152.     if (NULL!=hBmp)
  1153.         {
  1154.         //Draw the POLYLINEDATA into the bitmap.
  1155.         hObj=SelectObject(hMemDC, hBmp);
  1156.         Draw(hMemDC, FALSE, TRUE);
  1157.         SelectObject(hMemDC, hObj);
  1158.         }
  1159.  
  1160.     DeleteDC(hMemDC);
  1161.     ReleaseDC(m_hWnd, hDC);
  1162.  
  1163.     return hBmp;
  1164.     }
  1165.  
  1166.  
  1167.  
  1168.  
  1169.  
  1170.  
  1171.  
  1172. /*
  1173.  * CPolyline::RenderMetafile
  1174.  *
  1175.  * Purpose:
  1176.  *  Renders the current image of the Polyline into a metafile.
  1177.  *
  1178.  * Parameters:
  1179.  *  None
  1180.  *
  1181.  * Return Value:
  1182.  *  HMETAFILE       Handle to the newly created metafile.
  1183.  */
  1184.  
  1185. HMETAFILE CPolyline::RenderMetafile(void)
  1186.     {
  1187.     HDC         hDC;
  1188.     HMETAFILE   hMF;
  1189.     RECT        rc;
  1190.  
  1191.     //Create a memory metafile and return its handle.
  1192.     hDC=(HDC)CreateMetaFile(NULL);
  1193.     hMF=NULL;
  1194.  
  1195.     if (NULL!=hDC)
  1196.         {
  1197.         /*
  1198.          * This is absolutely essential to the metafile so it
  1199.          * can be scaled in the clipboard and any destination
  1200.          * application.
  1201.          */
  1202.         SetMapMode(hDC, MM_ANISOTROPIC);
  1203.         GetClientRect(m_hWnd, &rc);
  1204.         SetWindowOrg(hDC, 0, 0);
  1205.         SetWindowExt(hDC, rc.right, rc.bottom);
  1206.  
  1207.         Draw(hDC, TRUE, TRUE);
  1208.         hMF=CloseMetaFile(hDC);
  1209.         }
  1210.  
  1211.     return hMF;
  1212.     }
  1213.  
  1214.  
  1215.  
  1216.  
  1217.  
  1218.  
  1219. /*
  1220.  * CPolyline::RenderMetafilePict
  1221.  *
  1222.  * Purpose:
  1223.  *  Renders the current Polyline into a METAFILEPICT structure in
  1224.  *  global memory.
  1225.  *
  1226.  * Parameters:
  1227.  *  None
  1228.  *
  1229.  * Return Value:
  1230.  *  HGLOBAL         Memory containing the METAFILEPICT structure.
  1231.  */
  1232.  
  1233. HGLOBAL CPolyline::RenderMetafilePict(void)
  1234.     {
  1235.     HGLOBAL         hMem;
  1236.     HMETAFILE       hMF;
  1237.     LPMETAFILEPICT  pMF;
  1238.     RECT            rc;
  1239.  
  1240.  
  1241.     //Get the metafile
  1242.     hMF=RenderMetafile();
  1243.  
  1244.     if (NULL==hMF)
  1245.         return NULL;
  1246.  
  1247.     //Allocate the METAFILEPICT structure.
  1248.     hMem=GlobalAlloc(GMEM_DDESHARE | GMEM_MOVEABLE, sizeof(METAFILEPICT));
  1249.  
  1250.     if (NULL==hMem)
  1251.         {
  1252.         DeleteMetaFile(hMF);
  1253.         return NULL;
  1254.         }
  1255.  
  1256.     /*
  1257.      * Global lock only fails in PMODE if the selector is invalid
  1258.      * (like it was discarded) or references a 0 length segment,
  1259.      * neither of which can happen here.
  1260.      */
  1261.     pMF=(LPMETAFILEPICT)GlobalLock(hMem);
  1262.  
  1263.     pMF->hMF=hMF;
  1264.     pMF->mm=MM_ANISOTROPIC;
  1265.  
  1266.     //Insert the extents in MM_HIMETRIC units.
  1267.     GetClientRect(m_hWnd, &rc);
  1268.     RectConvertMappings(&rc, FALSE);
  1269.     pMF->xExt=rc.right;
  1270.     pMF->yExt=rc.bottom;
  1271.  
  1272.     GlobalUnlock(hMem);
  1273.     return hMem;
  1274.     }
  1275.  
  1276.  
  1277.  
  1278.  
  1279.  
  1280.  
  1281. /*
  1282.  * CPolyline::RectConvertMappings
  1283.  *
  1284.  * Purpose:
  1285.  *  Converts the contents of a rectangle from device (MM_TEXT) or
  1286.  *  HIMETRIC to the other.
  1287.  *
  1288.  * Parameters:
  1289.  *  pRect           LPRECT containing the rectangle to convert.
  1290.  *  fToDevice       BOOL TRUE to convert from HIMETRIC to device,
  1291.  *                  FALSE to convert device to HIMETRIC.
  1292.  *
  1293.  * Return Value:
  1294.  *  None
  1295.  */
  1296.  
  1297. void CPolyline::RectConvertMappings(LPRECT pRect, BOOL fToDevice)
  1298.     {
  1299.     HDC      hDC;
  1300.     int      iLpx, iLpy;
  1301.  
  1302.     if (NULL==pRect)
  1303.         return;
  1304.  
  1305.     hDC=GetDC(NULL);
  1306.     iLpx=GetDeviceCaps(hDC, LOGPIXELSX);
  1307.     iLpy=GetDeviceCaps(hDC, LOGPIXELSY);
  1308.     ReleaseDC(NULL, hDC);
  1309.  
  1310.     if (fToDevice)
  1311.         {
  1312.         pRect->left=MulDiv(iLpx, pRect->left, HIMETRIC_PER_INCH);
  1313.         pRect->top =MulDiv(iLpy, pRect->top , HIMETRIC_PER_INCH);
  1314.  
  1315.         pRect->right =MulDiv(iLpx, pRect->right,  HIMETRIC_PER_INCH);
  1316.         pRect->bottom=MulDiv(iLpy, pRect->bottom, HIMETRIC_PER_INCH);
  1317.  
  1318.        #ifdef NEVER
  1319.         /*
  1320.          * In this conversion we may get situations where the top
  1321.          * coordinate is larger than the bottom, which messes us up.
  1322.          */
  1323.         if (pRect->bottom < pRect->top)
  1324.             {
  1325.             iLpy=pRect->top;
  1326.             pRect->top=pRect->bottom;
  1327.             pRect->bottom=iLpy;
  1328.             }
  1329.        #endif
  1330.         }
  1331.     else
  1332.         {
  1333.         pRect->left=MulDiv(pRect->left, HIMETRIC_PER_INCH, iLpx);
  1334.         pRect->top =MulDiv(pRect->top , HIMETRIC_PER_INCH, iLpy);
  1335.  
  1336.         pRect->right =MulDiv(pRect->right,  HIMETRIC_PER_INCH, iLpx);
  1337.         pRect->bottom=MulDiv(pRect->bottom, HIMETRIC_PER_INCH, iLpy);
  1338.         }
  1339.  
  1340.     return;
  1341.     }
  1342.